import 'package:flutter/material.dart';
import 'package:silver_flutter/chapter_page.dart';
import 'package:silver_flutter/model/book.dart';
import 'package:silver_flutter/model/chapter.dart';
import 'package:silver_flutter/api.dart' as api;
import 'package:silver_flutter/widget/fav_button.dart';

class BookPage extends StatefulWidget {
  final Book book;

  BookPage({Key key, this.book}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return new _BookPageState();
  }
}

class _BookPageState extends State<BookPage> {
  final _tabs = ['详情', '目录'];

  @override
  Widget build(BuildContext context) {
    return new DefaultTabController(
      length: _tabs.length,
      child: new Scaffold(
        body: new NestedScrollView(
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return <Widget>[
              new SliverOverlapAbsorber(
                handle:
                    NestedScrollView.sliverOverlapAbsorberHandleFor(context),
                child: new SliverAppBar(
                  title: new Text(widget.book.title),
                  flexibleSpace: new FlexibleSpaceBar(
                    background: new Container(
                      decoration: new BoxDecoration(
                        color: const Color(0xff000000),
                        image: new DecorationImage(
                          fit: BoxFit.cover,
                          colorFilter: new ColorFilter.mode(
                              Colors.black.withOpacity(0.5), BlendMode.dstATop),
                          image: new NetworkImage(
                            widget.book.coverImageUrl,
                          ),
                        ),
                      ),
                    ),
                  ),
                  actions: <Widget>[
                    new FavButton(bookId: widget.book.id,),
                  ],
                  pinned: true,
                  floating: false,
                  expandedHeight: 200.0,
                  forceElevated: innerBoxIsScrolled,
                  bottom: new TabBar(
                    indicatorWeight: 3.0,
                    indicatorSize: TabBarIndicatorSize.label,
                    tabs: _tabs
                        .map((String name) => new Tab(text: name))
                        .toList(),
                  ),
                ),
              ),
            ];
          },
          body: new TabBarView(children: [
            new Builder(builder: (BuildContext context) {
              return new CustomScrollView(
                  key: new PageStorageKey<String>(_tabs[1]),
                  slivers: <Widget>[
                    new SliverOverlapInjector(
                      handle: NestedScrollView
                          .sliverOverlapAbsorberHandleFor(context),
                    ),
                    new SliverPadding(
                      padding: const EdgeInsets.all(10.0),
                      sliver: new SliverToBoxAdapter(
                        child: new Text(widget.book.description),
                      ),
                    ),
                  ]);
            }),
            new FutureBuilder<List<Chapter>>(
              future: api.fetchChapters(widget.book.id),
              builder: (context, snapshot) {
                return new CustomScrollView(
                  slivers: <Widget>[
                    new SliverOverlapInjector(
                      handle: NestedScrollView
                          .sliverOverlapAbsorberHandleFor(context),
                    ),
                    () {
                      if (snapshot.hasData) {
                        return new SliverPadding(
                            padding: EdgeInsets.all(10.0),
                            sliver: new SliverGrid.count(
                                crossAxisSpacing: 10.0,
                                crossAxisCount: 5,
                                mainAxisSpacing: 10.0,
                                childAspectRatio: 2.0 / 1.0,
                                children: snapshot.data
                                    .map(
                                      (chapter) => new RaisedButton(
                                            onPressed: () {
                                              Navigator.push(
                                                context,
                                                new MaterialPageRoute(
                                                    builder: (context) =>
                                                        new ChapterPage(
                                                          bookId:
                                                              widget.book.id,
                                                          chapter: chapter,
                                                        )),
                                              );
                                            },
                                            child: new Text(
                                                chapter.number.toString()),
                                          ),
                                    )
                                    .toList()));
                      } else if (snapshot.hasError) {
                        return new SliverToBoxAdapter(
                            child: new Text("${snapshot.error}"));
                      }

                      // By default, show a loading spinner
                      return new SliverToBoxAdapter(
                          child: new Center(
                              child: new CircularProgressIndicator()));
                    }(),
                  ],
                );
              },
            ),
          ]),
        ),
      ),
    );
  }
}
